package com.cinematicketscaning.app.controller

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.cinematicketscaning.app.R
import com.cinematicketscaning.app.adapter.MovieInfoAdapter
import com.cinematicketscaning.app.bean.MovieItem
import com.cinematicketscaning.app.controller.base.BaseActivity
import com.cinematicketscaning.app.dialog.TipsDialog
import com.cinematicketscaning.app.utils.Utils
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : BaseActivity() {

    var mExitTime = 0L
    var selectedItemPosition = -1
    private lateinit var adapter: MovieInfoAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        adapter = MovieInfoAdapter(this, listOf(MovieItem(), MovieItem(), MovieItem(), MovieItem(), MovieItem(), MovieItem()))
        listview.adapter = adapter

        img_check_tickets.setOnClickListener {
            if (selectedItemPosition != -1) {
                startActivity(Intent(this, MovieActivity::class.java))
            } else {
                Utils.showTwoBtnDialog(supportFragmentManager, "提示", "请先选择一场电影", "取消", "确定", object : TipsDialog.ClickEvent {
                    override fun clickLeft() {
                    }

                    override fun clickRight() {
                    }
                })
            }
        }
        img_more.setOnClickListener {
            startActivity(Intent(this, MovieListActivity::class.java))
        }
        img_help.setOnClickListener {
            startActivity(Intent(this, CompanyActivity::class.java))
        }
        listview.setOnItemClickListener { parent, view, position, id ->
            for (i in 0 until adapter.datas.size) {
                adapter.datas[i].isSelected = i == position
            }
            selectedItemPosition = position
            adapter.notifyDataSetChanged()
        }
    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            mExitTime = System.currentTimeMillis()
            Toast.makeText(this@MainActivity,"请再次点击退出APP", Toast.LENGTH_SHORT).show()
        } else {
            finish()
        }
    }

    override fun isUseTimeReceiver(): Boolean {
        return true
    }

    override fun updateTime() {
        val calendar = Utils.initCalendar()
        val hours = calendar.get(Calendar.HOUR).toString()
        val minutes = calendar.get(Calendar.MINUTE).toString()
        if (hours.length > 1) {
            tv_time1.text = hours[0].toString()
            tv_time2.text = hours[1].toString()
        } else {
            tv_time1.text = "0"
            tv_time2.text = hours
        }
        if (minutes.length > 1) {
            tv_time3.text = minutes[0].toString()
            tv_time4.text = minutes[1].toString()
        } else {
            tv_time3.text = "0"
            tv_time4.text = minutes
        }
    }

}
